<?php
namespace app\zhonghang\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cache;

require_once (APP_PATH . 'api/RewardApi.php');
class Car extends Base
{
    public function _initialize()
    {
        parent::_initialize();

    }

    /*
     *购物车列表
     */
    public function cartList()
    {
        $member=$this->member;
        $where['user_id']=$member['user_id'];
        $list=DB::name('Cart')->where($where)->order('add_time desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['goods_pic']=DB::name('goods')->where('goods_id',$list[$k]['goods_id'])->value('original_img');
            $list[$k]['goods_price']=DB::name('Goods_stock')->where('goods_id',$list[$k]['goods_id'])->where('color',$list[$k]['spec_one'])->where('label',$list[$k]['spec_two'])->value('price');
        }
        if($list){
            $data['info']='该用户购物车列表';
            $data['status']='1';
            $data['data']=$list;
            return json($data);
        }else{
            $data['info']='暂无数据';
            $data['status']='-1';
            $data['data']=array();
            return json($data);
        }
    }

    public function addCart(){
        $member=$this->member;
        $arr['user_id']=$member['user_id'];
        $arr['goods_id']=input('goods_id');
        $arr['spec_one']=input('color');
        $arr['spec_two']=input('chima');
        $arr['goods_num']=input('num');
        $bool=DB::name('Cart')->where('goods_id',$arr['goods_id'])->where('spec_one',$arr['spec_one'])->where('spec_two',$arr['spec_two'])->find();
        if($bool){
            $data['info']='该商品已存在购物车';
            $data['status']='-4';
            $data['data']=array();
            return json($data);exit;
        }
        $kucun=DB::name('Goods_stock')->where('goods_id',$arr['goods_id'])->where('color',$arr['spec_one'])->where('label',$arr['spec_two'])->value('num');
        if($kucun<$arr['goods_num']){
            $data['info']='库存不足';
            $data['status']='-2';
            $data['data']=array();
            return json($data);exit;
        }
        if($arr['goods_id']==null || $arr['spec_one']==null || $arr['spec_two']==null){
            $data['info']='请选择商品规格';
            $data['status']='-1';
            return json($data);exit;
        }
        $arr['add_time']=time();
        $arr['goods_name']=DB::name('goods')->where('goods_id',$arr['goods_id'])->value('goods_name');
        $res=DB::name('Cart')->insert($arr);
        if($res){
            $data['info']='加入购物车成功';
            $data['status']='1';
            $data['data']=array();
            return json($data);
        }else{
            $data['info']='系统繁忙,请稍后再试';
            $data['status']='-3';
            $data['data']=array();
            return json($data);
        }
    }
    /*
     *删除购物车内商品
     **/
    public function del(){
        $where['id'] = input('id');
        $bool = DB::name('Cart')->where($where)->delete();
        if($bool){
            $data['info']='删除成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info']='删除失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
}